package com.wntms.wy.service.impl;

import com.github.pagehelper.PageHelper;
import com.wntms.wy.dao.SalesmanDao;
import com.wntms.wy.pojo.*;
import com.wntms.wy.pojo.sjhz.DriverBill_s;
import com.wntms.wy.service.SalesmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//业务员验收、货物回执单的增删改查
@Service
public class SalesmanServiceImpl implements SalesmanService {
    @Autowired
    private SalesmanDao salesmanDao;

    @Override
    @Transactional
    public Boolean panduanFC(String fid) {
        //先判断是否填写过验收单
        //findSACount
        int saCount = salesmanDao.findSACount(fid);
        int i = salesmanDao.freightContractCountFindByFid(fid);
        if (i <1){
            //说明没有货运单
            return false;
        }else {
           return true;
        }
        //return i + saCount<2?false:true;
    }

    //业务员增加验收单的操作
    @Override
    @Transactional
    public Boolean addSalesmanAcount(SalesmanAccept salesmanAccept) {
        //前端会传个SalesmanAccept过来

        System.out.println(salesmanAccept + ".." + "什么贵");
        //先通过运输合同号通过fid去查得到的
        String cc = salesmanDao.fidFindCid(salesmanAccept.getFid());
        salesmanAccept.setCid(cc);
        //插入验收记录
        int count = salesmanDao.salesmanAcceptAdd(salesmanAccept);
        System.out.println(salesmanAccept + ".." + "什么贵");
        //根据istransit的值判断是否需要中转
        FreightContract freightContract = salesmanDao.freightContractFindByFid(salesmanAccept.getFid());
        int i = 0;
        if(salesmanAccept.getIstransit()){
            //说明需要中转
            i = salesmanDao.updateFreightStatus1(freightContract.getFsid());
        }else {
            //不需要中转
            i = salesmanDao.updateFreightStatus2(freightContract.getFsid());
        }
        return count + i < 2 ? false : true;
    }

    //业务员删除验收记录
    @Transactional
    public Boolean salesmanAcountDeleteById(Integer id){
        //来个判断如果填了司机回执单就不能删除
        int dd = salesmanDao.findDriverBillById(id);
        if (dd == 1){
            return false;
        }else {
            int count = salesmanDao.salesmanAcceptDeleteById(id);
            //System.out.println("我真的删除了");

            return count < 1 ? false : true;
        }



    }

    @Override
    @Transactional
    public Boolean salesmanAcountUpdate(SalesmanAccept salesmanAccept) {
        int count = salesmanDao.salesmanAcountUpdate(salesmanAccept);
        //根据istransit的值判断是否需要中转
        FreightContract freightContract = salesmanDao.freightContractFindByFid(salesmanAccept.getFid());
        int i = 0;
        if(salesmanAccept.getIstransit()){
            //说明需要中转
            i = salesmanDao.updateFreightStatus1(freightContract.getFsid());
        }else {
            //不需要中转
            i = salesmanDao.updateFreightStatus2(freightContract.getFsid());
        }
        return count + i < 2 ? false : true;
    }

    @Override
    public List<SalesmanAccept> slesmanAcountFindAll() {
        List<SalesmanAccept> list = salesmanDao.salesmanAcceptFindAll();


        return list;
    }

    @Override
    public List<SalesmanAccept> SAfindByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);

        List<SalesmanAccept> list = salesmanDao.salesmanAcceptFindAll();


        return list;
    }
    /*CREATE TABLE `driver_bill` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `cid` varchar(50) DEFAULT NULL COMMENT '运输合同表id',
            `diid` int(11) DEFAULT NULL COMMENT '司机信息表id',
            `fid` varchar(50) DEFAULT NULL COMMENT '货运单合同表id',
            `said` int(11) DEFAULT NULL COMMENT '业务员验收表',
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='司机回执'*/

    //司机回执单操作
    //填写之前确定是否验收成功，有无这个货运单号
    @Transactional
    public Boolean panduanByFid(String fid){
        int i1 = salesmanDao.freightContractCountFindByFid(fid);

        if (i1>= 1){
            //说明由货运单
            int i = salesmanDao.selectSalesmanAcceptByFid(fid);
            if (i>=1){
                //说明有这个验收单
                Boolean b1 = salesmanDao.selectSalesmanAcceptIspass(fid);
                if (b1){
                    //说明验收成功，可以填回执单
                    int dd = salesmanDao.findDriverBillCcountByFid(fid);
                    if (dd >0) {
                        System.out.println("错误");
                        return false;

                    }else {
                        return true;
                    }
                    //return dd<1? true:false;
                }else{
                    System.out.println("错误2");
                    //说明验收不成功
                    return false;
                }
            }else {
                //说明没有这个货运单验收单，可以填
                System.out.println("错误3");
                return false;
            }


        }else {
            //说明没有这个货运单，不能填写回执单
            System.out.println("错误4");
            return false;
        }

    }
    @Override
    @Transactional
    public Boolean addDriverReceipt(String fid) {
        //获取cid和diid
        CarrigeContract carrigeContract = salesmanDao.carriageContractFindByFid(fid);//2
        //获取said
        SalesmanAccept salesmanAccept = salesmanDao.salesmanAcceptFindByFid(fid);//2 =>2

        DriverBill driverBill = new DriverBill();
        driverBill.setCid(carrigeContract.getCid());//2
        driverBill.setDiid(carrigeContract.getDiid());//2
        driverBill.setFid(fid);//2
        driverBill.setSaid(salesmanAccept.getId());//2
        //System.out.println("我到service层了" + carrigeContract.getCid() + "..." +carrigeContract.getDiid() +"..." + fid + "..." + salesmanAccept.getId());
        int count = salesmanDao.insertDriverBill(driverBill);
        if(count < 1) {
            //增加失败
            return false;
        }else {
            //增加成功
            FreightContract fc1 = salesmanDao.freightContractFindByFid(fid);
            //修改运输单里的状态 为4代表回执
            //int i = salesmanDao.updateFreightStatus3(fc1.getFsid());
            //修改司机信息表里的运单状态 -1
            int i1 = salesmanDao.updateDriverInformation(carrigeContract.getDiid());

            /*int i = salesmanDao.updateCarriageContract(carrigeContract.getCid());
            return i<1? false:true;*/
            return i1<1? false:true;
        }

    }

    @Override
    public Boolean deleteDriverReceipt(Integer id) {
        int i = salesmanDao.deleteDriverBillById(id);
        return i<1?false:true;
    }

    @Override
    @Transactional
    public Boolean updateDriverReceipt(DriverBill_s driverBill_s) {
        //现根据前端传过来的数据中的fid找到货运合同表
        FreightContract freightContract = salesmanDao.freightContractFindByFid(driverBill_s.getFid());
        CustomerIn customerIn = new CustomerIn();
        customerIn.setId(freightContract.getCoid());
        customerIn.setName(driverBill_s.getConsignee_name());
        customerIn.setTel(driverBill_s.getConsignee_number());
        int i = salesmanDao.updateCustomerIn(customerIn);
        System.out.println("我又进来了");
        DriverSettle driverSettle = new DriverSettle();
        driverSettle.setCid(driverBill_s.getCid());
        driverSettle.setIsSettle(driverBill_s.getIsSettle());
        int i1 = salesmanDao.updateDriverSettle(driverSettle);

        return i+i1 < 2 ? false:true;
    }

    @Override
    @Transactional
    public List<DriverBill_s> driverBillsFindAll(Integer currentPage,Integer pageSize) {


        List<DriverBill> driver_bills = salesmanDao.driverBillFindAll();
        PageHelper.startPage(currentPage,pageSize);
        //System.out.println("我又进来了" + driver_bills);
        List<DriverBill_s> list = new ArrayList<>();
        for (DriverBill driverBill : driver_bills) {
            //System.out.println("让我看看" + driverBill);
            DriverBill_s bill_s = new DriverBill_s();
            //获取运输合同表数据
            CarrigeContract carrigeContract = salesmanDao.carrigeContractFindByCid(driverBill.getCid());
            //cid、fid、起运时间、运费、保险费
            bill_s.setCid(carrigeContract.getCid());
            bill_s.setFid(carrigeContract.getFid());
            bill_s.setShipping_time(carrigeContract.getShippingtime());
            bill_s.setFare(carrigeContract.getFare());
            bill_s.setInsurancefare(carrigeContract.getInsurance());
            //设置回执表id主键值
            bill_s.setId(driverBill.getId());

            //司机结算表
            Boolean aBoolean = salesmanDao.driver_SettleFindByCid(driverBill.getCid());
            bill_s.setIsSettle(aBoolean);

            //获取司机信息表
            //System.out.println(driverBill.getDiid() + "..." + "到底是什么");
            DriverInformation driverInformation = salesmanDao.driverFindByDiid(driverBill.getDiid());
            bill_s.setDriver_name(driverInformation.getName());
            //收获客户信息表
            FreightContract freightContract = salesmanDao.freightContractFindByFid(carrigeContract.getFid());
            CustomerIn customerIn = salesmanDao.customerInFindByCoid(freightContract.getCoid());

            bill_s.setConsignee_name(customerIn.getName());
            bill_s.setConsignee_number(customerIn.getTel());
            //查询客户验收表
            int cc = salesmanDao.findCustomerCountByFid(driverBill.getFid());
            if (cc<1){
                bill_s.setConsignee_acceptance(false);
            }else {
                Boolean aBoolean1 = salesmanDao.customerAcceptFindByFid(driverBill.getFid());
                bill_s.setConsignee_acceptance(aBoolean1);
            }


            //先判断业务员有没有验收
            int ss = salesmanDao.findSalesmanCountByFid(driverBill.getFid());

            if (ss<1){
                bill_s.setIspass(false);
                bill_s.setArrive_time(null);
            }else {
                //获取业务员验收表
                SalesmanAccept salesmanAccept = salesmanDao.salesmanAcceptFindBySaid(driverBill.getSaid());
                bill_s.setIspass(salesmanAccept.getIspass());
                bill_s.setArrive_time(salesmanAccept.getArrivetime());
            }


            list.add(bill_s);
        }
        //System.out.println(driver_bills);

        return list;
    }


}
